package com.nuo.modules.surveyresult.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuo.common.excel.po.ExportListPo;
import com.nuo.common.excel.service.ExcelService;
import com.nuo.common.utils.InfoUtils;
import com.nuo.common.utils.PageUtils;
import com.nuo.common.utils.Query;
import com.nuo.common.utils.R;
import com.nuo.modules.student.entity.StudentEntity;
import com.nuo.modules.student.service.StudentService;
import com.nuo.modules.surveyresult.dao.SurveyResultDao;
import com.nuo.modules.surveyresult.entity.SurveyResultEntity;
import com.nuo.modules.surveyresult.po.OkPo;
import com.nuo.modules.surveyresult.service.SurveyResultService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;



@Service("surveyResultService")
public class SurveyResultServiceImpl extends ServiceImpl<SurveyResultDao, SurveyResultEntity> implements SurveyResultService {

    @Autowired
    InfoUtils<StudentEntity> infoUtils;

    @Autowired
    StudentService studentService;

    @Autowired
    private ExcelService<SurveyResultEntity, SurveyResultService> excelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String)params.get("username");
        IPage<SurveyResultEntity> page = this.page(
                new Query<SurveyResultEntity>().getPage(params),
                new LambdaQueryWrapper<SurveyResultEntity>()
                .like(StringUtils.isNotBlank(username),SurveyResultEntity::getName,username)
        );

        return new PageUtils(page);
    }

    @Override
    public R importList(MultipartFile file) {
        return excelService.importList(file, SurveyResultEntity.class, this);
    }

    @Override
    public R exportList(ExportListPo<SurveyResultEntity> po) {
        return excelService.exportList(po, SurveyResultEntity.class);
    }

    @Override
    public R exportAll() {
        return excelService.exportAll(SurveyResultEntity.class, this);
    }

    @Override
    public R getList(Map<String, Object> params) {
        StudentEntity student = infoUtils.getInfoByToken((String) params.get("token"), 1, studentService);
        List<SurveyResultEntity> list = list(new LambdaQueryWrapper<SurveyResultEntity>()
                        .eq(SurveyResultEntity::getStuId, student.getStudentId())
                .orderByDesc(SurveyResultEntity::getCreateTime)
        );
        return R.ok().put("list", list);
    }

    @Override
    public R ok(OkPo po) {
        infoUtils.getIdByToken(po.getToken(), 1);
        SurveyResultEntity resultEntity = new SurveyResultEntity();
        resultEntity.setId(po.getId());
        resultEntity.setStatus(1);
        updateById(resultEntity);
        return R.ok();
    }

    @Override
    public void saveOrUpdateBatch(List<SurveyResultEntity> list, Class<SurveyResultEntity> clazz) {
        saveBatch(list);
    }
}
